package site.yunnong.atvris.admin.controller;

import com.google.gson.Gson;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.yunnong.atvris.common.api.Page;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.api.ResultVO;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.service.OssService;
import site.yunnong.atvris.common.enumeration.Constant;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;

/**
 * @author JinghaoZhang
 * @date 2021年-04月-29日
 */
@Api(tags = "上传文件")
@RestController
@RequestMapping("/admin/oss")
public class SysOssController {

    @Resource
    private OssService ossService;
    @Resource
    private SysConfigService sysConfigService;
    
    private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;

    /**
     * 列表
     */
    @GetMapping("/list")
    public ResultVO list(@RequestParam Map<String, Object> params){
        Page page = ossService.queryPage(params);
        return new ResultVO<>().success(page);
    }


    /**
     * 云存储配置信息
     */
    @GetMapping("/config")
    public ResultVO config(){
        CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);

        return new ResultVO<>().success(config);
    }


    /**
     * 保存云存储配置信息
     */
    @PostMapping("/saveConfig")
    public ResultVO saveConfig(@RequestBody CloudStorageConfig config){

        if(config.getType() == Constant.CloudService.QINIU.getValue()){
            //校验七牛数据
            ValidatorUtils.validateEntity(config, QiniuGroup.class);
        }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
            //校验阿里云数据
            ValidatorUtils.validateEntity(config, AliyunGroup.class);
        }else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
            //校验腾讯云数据
            ValidatorUtils.validateEntity(config, QcloudGroup.class);
        }

        sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));

        return new ResultVO<>().success();
    }


    /**
     * 上传文件
     */
    @PostMapping("/upload")
    public ResultVO upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL, "上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        //保存文件信息
        SysOss oss = new SysOss();
        oss.setUrl(url);
        oss.setCreateDate(LocalDateTime.now());
        ossService.save(oss);

        return new ResultVO<>().success(url);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    public ResultVO delete(@RequestBody Long[] ids){
        ossService.removeByIds(Arrays.asList(ids));

        return new ResultVO<>().success();
    }
}
